package com.cman777.cvideo.client.controller;

import com.cman777.cvideo.client.util.DialogBuilder;
import com.cman777.springc.common.helper.StringTemplateUtil;
import com.cman777.video.service.vs.IVideoSource;
import com.cman777.video.service.vs.VS147;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextArea;
import javafx.event.*;
import javafx.scene.input.*;
import javafx.fxml.FXML;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.fxml.Initializable;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class IndexController implements Initializable {
    public static String tempHtml = System.getProperties().getProperty("user.home") + "\\cvideo\\tempHtmls.html";
    public static String m3u8tpl = null;
    public static String normaltpl = null;
    static {
        InputStream m3u8tplIn = IndexController.class.getClassLoader().getResourceAsStream("tpl/m3u8tpl.html");
        InputStream normaltplIn = IndexController.class.getClassLoader().getResourceAsStream("tpl/normaltpl.html");
        try {
            m3u8tpl = IOUtils.toString(m3u8tplIn, Charset.defaultCharset());
            normaltpl = IOUtils.toString(normaltplIn, Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @FXML
    private JFXTextArea source;

    @FXML
    private JFXButton analysis;

    @FXML
    private JFXTextArea result;

    @FXML
    private JFXButton copy;

    @Override
    public void initialize(URL event, ResourceBundle rb) {
    }

    @FXML
    private void analysisSource(ActionEvent event) {
        IVideoSource videoSource = new VS147();
        String resultUrl = null;
        try {
            resultUrl =videoSource.getDownLoadUrlOfGenuineUrl(source.getText());
        }catch (Exception e){
                new DialogBuilder(analysis).setTitle("错误").setMessage("解析失败").setNegativeBtn("确定").create();
                return;
        }
        result.setText(resultUrl);
    }

    @FXML
    private void copyResult(ActionEvent event) throws IOException {
        File tempFile = new File(tempHtml);
        if(!tempFile.exists()){
            FileUtils.forceMkdirParent(tempFile);
            tempFile.createNewFile();
        }
        OutputStream outputStream =  new FileOutputStream(tempFile);
        String url = result.getText();

        if(url.endsWith("m3u8")){
            Map<String,String> map = new HashMap<>();
            map.put("M3U8Url",url);
            String html = StringTemplateUtil.templateEngine(m3u8tpl,map);
            IOUtils.write(html,outputStream,Charset.defaultCharset());
        }else{
            Map<String,String> map = new HashMap<>();
            map.put("notM3U8Url",url);
            String html = StringTemplateUtil.templateEngine(normaltpl,map);
            IOUtils.write(html,outputStream,Charset.defaultCharset());
        }
        if (java.awt.Desktop.isDesktopSupported()) {
            try {
                // 创建一个URI实例
                String uriS = "file:///".concat(tempHtml.replace("\\","/"));
                java.net.URI uri = java.net.URI.create(uriS);
                // 获取当前系统桌面扩展
                java.awt.Desktop dp = java.awt.Desktop.getDesktop();
                // 判断系统桌面是否支持要执行的功能
                if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
                    // 获取系统默认浏览器打开链接
                    dp.browse(uri);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }




    }

    protected static void setClipboardText(String writeMe) {
        Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(writeMe);
        sysc.setContents(tText, null);
    }

}
